package com.lovo.csc.service.impl;

import com.lovo.csc.entity.Vendororder;
import com.lovo.csc.service.IVendororderService;
import com.lovo.psc.dto.OrderDto;
import com.lovo.util.MQUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class VendorOrderMQService {
    @Autowired
    IVendororderService vendororderService;
    @RabbitListener(queues = MQUtil.SupplyOrderCscQueue)
    public void addOrder(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, OrderDto dto) throws IOException {
        if (null!=dto){
            Vendororder vendororder = new Vendororder();
            vendororder.setVendorcode(dto.getOrderNumber());
            vendororder.setVendorname(dto.getSupplierName());
            int total = dto.getCommodityNum() * dto.getCommodityPrice();
            vendororder.setVendorordertotal(total+"");
            vendororder.setVendororderstate("0");
            vendororderService.save(vendororder);
            channel.basicAck(tag,false);
        }
    }
}
